#!/bin/bash

source lib.sh
set_cgroup_rights

mylogdir="/data/oc-ops/io/latency"
mycurrdir="$(pwd)"
remain=0

while getopts 'l:r:h' OPT; do
        case $OPT in
                l)      logdir="$OPTARG"
                        ;;
                r)      remain="$OPTARG"
                        ;;
                h)      echo "$usage"
                        exit $?
                        ;;
                ?)      echo "Invalid option: $OPT" >&2
                        echo "$usage"
                        exit 1
                        ;;
        esac
done

if [[ $mycurrdir == $mylogdir && $remain == 0 ]]; then
	rm -rf *.sh sd* nvme* vd* run_log 2>/dev/null 
fi


kill_times=5
echo "Killing previous blktrace processes, please wait $kill_times seconds ..."

for ((i=0; i<$kill_times; i++))
do
	#echo killing previous blktrace...
	pgid=`ps fjx | grep blktrace | grep '/dev' | grep -E 'nvme|sd|vd' | awk '{ print $3}'`
	for i in $pgid
	do
		pid=`ps fjx | grep $i | awk '{ print $2}'`
		for j in $pid
		do
			#echo killing ${j}
			kill $j 1>/dev/null 2>&1
		done
	done
	sleep 1
done
